/**
 * 
 */
package com.gs.logviewer.core.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.gs.utils.text.StringUtil;

/**
 * @author Sabuj
 *
 */
public class ConversionPatternParser {

	private final Logger logger = Logger.getLogger(ConversionPatternParser.class);
	
	public static final char CATEGORY = 'c';
	public static final char CLASS_NAME = 'C';
	public static final char DATE = 'd';
	public static final char FILE_NAME = 'F';
	public static final char LOCATION = 'l';
	public static final char LINE_NUMBER = 'L';
	public static final char MESSAGE = 'm';
	public static final char METHOD_NAME = 'M';
	public static final char LINE_SEPARATOR = 'n';
	public static final char PRIORITY = 'p';
	public static final char MILLISECONDS = 'r';
	public static final char THREAD_NAME = 't';
	public static final char NDC = 'x';
	public static final char MDC = 'X';
	public static final char ESCAPE_SEQUENCE = '%';
	public static final char OPEN_BRACE = '{';
	public static final char CLOSE_BRACE = '}';
	public static final char LEFT_JUSTIFICATION_FLAG = '-';
	public static final char DOT = '.';
	public static final char SPACE = ' ';

	public static final String[] ADI_DATE_FORMATS = { 
		"ABSOLUTE", 
		"DATE",
		"ISO8601" };
	public static final String ABSOLUTE_TIME_DATE_FORMAT = "HH:mm:ss,SSS";
	public static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
	public static final String DATE_TIME_DATE_FORMAT = "dd MMM yyyy HH:mm:ss,SSS";
	
	public static final String CLASS_DEFAULT_DATE_FORMAT = "java.text.SimpleDateFormat";
	public static final String CLASS_ABSOLUTE_DATE_FORMAT = "org.apache.log4j.helpers.AbsoluteTimeDateFormat";
	public static final String CLASS_DATE_DATE_FORMAT = "org.apache.log4j.helpers.DateTimeDateFormat";
	public static final String CLASS_ISO8601_DATE_FORMAT = "org.apache.log4j.helpers.ISO8601DateFormat";
	
	public static final char[] SIMPLE_DATE_FORMAT_CHARS = {
		'd', // - DATE
        'H', // - HOUR_OF_DAY:0-based.  eg, 23:59 + 1 hour =>> 00:59
        'm', // - MINUTE
        's', // - SECOND
        'S', // - MILLISECOND
        'D', // - DAY_OF_YEAR
        'F', // - DAY_OF_WEEK_IN_MONTH
        'w', // - WEEK_OF_YEAR
        'W', // - WEEK_OF_MONTH
        'K', // - HOUR: 0-based.  eg, 11PM + 1 hour =>> 0 AM
        'z', // - ZONE_OFFSET
        'Z', // - ZONE_OFFSET ("-/+hhmm" form)
        'a', // - AM_PM
        'E', // - DAY_OF_WEEK
        'k', // - HOUR_OF_DAY: 1-based.  eg, 23:59 + 1 hour =>> 24:59
        'G', // - ERA
        'M', // - Month
        'y'  // - Year
        
	};
	
	public static final List<Character> TOKEN_CHAR_LIST = new ArrayList<Character>();
	public static final List<Character> SIMPLE_DATE_FORMAT_CHAR_LIST = new ArrayList<Character>();
	
	static{
		TOKEN_CHAR_LIST.add(CATEGORY);
		TOKEN_CHAR_LIST.add(CLASS_NAME);
		TOKEN_CHAR_LIST.add(DATE);
		TOKEN_CHAR_LIST.add(FILE_NAME);
		TOKEN_CHAR_LIST.add(LOCATION);
		TOKEN_CHAR_LIST.add(LINE_NUMBER);
		TOKEN_CHAR_LIST.add(LINE_SEPARATOR);
		TOKEN_CHAR_LIST.add(MESSAGE);
		TOKEN_CHAR_LIST.add(METHOD_NAME);
		TOKEN_CHAR_LIST.add(MILLISECONDS);
		TOKEN_CHAR_LIST.add(PRIORITY);
		TOKEN_CHAR_LIST.add(THREAD_NAME);
		TOKEN_CHAR_LIST.add(NDC);
		TOKEN_CHAR_LIST.add(MDC);
		
		
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[ 0]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[ 1]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[ 2]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[ 3]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[ 4]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[ 5]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[ 6]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[ 7]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[ 8]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[ 9]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[10]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[11]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[12]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[13]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[14]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[15]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[16]);
		SIMPLE_DATE_FORMAT_CHAR_LIST.add(SIMPLE_DATE_FORMAT_CHARS[17]);
	}
	
	
	
	private String conversionPattern;
	private String regularExpression;
	private Pattern pattern;
	
	private Stack<Character> patternCharacterStack;
	
	public ConversionPatternParser(String conversionPattern) {
		this.conversionPattern = conversionPattern;
		if(StringUtil.hasValidContent(conversionPattern)){
			parsePattern();
		}
	}
	
	private void parsePattern(){
		
	}
	
	
}
